const courses = {
    state: {
        id: '',//id
        title: '',//标题
        is_buy: "",//是否购买过
        price: '', //价格
        totalPeriods: '',//课时
        salesNum: '', //报名人数
        img: '', //图片
        teacherName: '',//老师名字
        detailTeach: '',//课程介绍
        courseCommentItem: [],  // 获取课程详情信息  评论
        courseChapterItem: [], // 获取课程详情信息 课程大纲
        years: '', // 开课时间 年
        startPlayDate: '', // 开课时间 月
        endPlayDate: '', // 开课时间 日
        collect: 0,//是否收藏
        collectId: 0,//收藏id
    },
    mutations: {
        //收藏
        COLLECT(state) {
            if (state.collect === 0) {
                this.$http.collect({
                    course_basis_id: state.id,
                    type: 1
                }).then(res => {
                    console.log(res)
                    if (res.data.code === 200) {
                        state.collectId = `${res.data.data}`
                        state.collect = 1

                        // this.dispatch('GETBASIS')
                    }
                })
            } else if (state.collect === 1) {
                this.$http.cancelCollect(`${state.collectId}/${state.collect}`).then(res => {
                    console.log('dd', res)
                    if (res.data.code === 200) {
                        state.collect = 0
                    }
                })
            }
        },
        //获取课程详情信息
        GETBASIS(state, data) {
            // console.log('data1::', data.info.course_details)
            state.title = data.info.title
            state.id = data.info.id
            state.collect = data.info.is_collect
            state.collectId = data.info.collect_id
            state.price = data.info.price
            state.totalPeriods = data.info.total_periods
            state.salesNum = data.info.sales_num
            state.img = data.teachers[0].avatar
            state.detailTeach = data.info.course_details
            state.teacherName = data.teachers[0].teacher_name
            state.years = data.info.start_play_year
            state.startPlayDate = data.info.start_play_date
            state.endPlayDate = data.info.end_play_date
            state.is_buy = data.info.is_buy//是否报名
        },
        // 获取课程详情信息  评论
        courseComment(state, data) {
            state.courseCommentItem = data
        },
        // 获取课程详情信息  课程大纲
        courseChapter(state, item) {
            state.courseChapterItem = item
        }
    },
    actions: {
        //获取课程详情信息
        GETBASIS(context, id) {
            this.$http.basis(id).then(res => {
                // console.log("data", res.data)

                context.commit('GETBASIS', res.data.data)
            })
        },
        //获取课程详情信息  评论
        courseComment(context, id) {
            this.$http.courseComment({
                id: id,
                limit: 10,
                page: 1
            }).then(res => {
                context.commit("courseComment", res.data.data.list)
            })
        },
        //获取课程详情信息  课程大纲
        courseChapter(context, id) {
            this.$http.courseChapter({id}).then(res => {
                // console.log('课程大纲', res.data.data)

                if (res.data.data.length === 1) {
                    res.data.data[0].teachers = [{teacher_name: 1}]
                }

                context.commit("courseChapter", res.data.data)
            })
        },
    },
    module: {}
};

export default courses;